package com.yonyou.pmclouds.altercategory.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.altercategory.entity.AlterCategoryVO;
import com.yonyou.pmclouds.altercategory.rmiitf.AlterCategoryMaintain;
import com.yonyou.pmclouds.altercategory.rmiitf.AlterCategoryQuery;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.service.CommonPropertiesProcessor;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 变更签证类别
 */
@RequestMapping("/altercategory")
@RestController
public class AlterCategoryController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private AlterCategoryMaintain maintain;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private AlterCategoryQuery categoryQuery;

    @Autowired
    private UserInfoQuery userQuery;
    @Autowired
    private CommonPropertiesProcessor propertiesProcessor;

    private static final String[] USER_ID_FIELDS = {"creator"};
    private static final String[] USER_NAME_FIELDS = {"creatorName"};

    /**
     * 提交
     */
    @RequestMapping(value = "/add")
    public AlterCategoryVO add(@RequestBody AlterCategoryVO alterCategoryVO) throws BusinessException {
        if (alterCategoryVO == null) return null;
        return maintain.insert(alterCategoryVO);
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/modify")
    public AlterCategoryVO modify(@RequestBody AlterCategoryVO alterCategoryVO) throws BusinessException {
        if (alterCategoryVO == null) return null;
        return maintain.update(alterCategoryVO);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    public String delete(@RequestBody AlterCategoryVO alterCategoryVO) throws BusinessException {
        if (alterCategoryVO == null) return null;
        return maintain.delete(alterCategoryVO.getPkAltercategory(), alterCategoryVO.getChangeFlag());
    }

    /**
     * 获取变更签证分类列表
     */
    @RequestMapping(value = "/getList")
    public AlterCategoryVO[] getList() throws BusinessException {
        AlterCategoryVO[] result = categoryQuery.queryCategoryListInfo(RuntimeEnvironment.getTenantId());
        userQuery.tranUserInfoField(result, USER_ID_FIELDS, USER_NAME_FIELDS);
        Map<String, List<AlterCategoryVO>> childMap = new HashMap<>();
        Map<String, String> codeMap = new HashMap<>();//主键编码映射
        for(AlterCategoryVO vo : result){
            codeMap.put(vo.getPkAltercategory(), vo.getScode());
        }
        for (AlterCategoryVO vo : result) {
            String parent = vo.getPkParent();
            if (StringUtils.isEmpty(parent)) {
                parent = StringUtils.EMPTY;
            }else{
                //前端需要子节点中存放父节点的编码
                vo.setPcode(codeMap.get(parent));
            }
            List<AlterCategoryVO> children = childMap.get(parent);
            if (children == null) {
                children = new ArrayList<>();
                childMap.put(parent, children);
            }
            children.add(vo);
        }
        for (AlterCategoryVO vo : result) {
            List<AlterCategoryVO> children = childMap.get(vo.getPkAltercategory());
            if (children != null) {
                vo.setChildCategory(children.toArray(new AlterCategoryVO[children.size()]));
            }
        }
        List<AlterCategoryVO> res = childMap.get(StringUtils.EMPTY);
        if (res == null) {
            return new AlterCategoryVO[0];
        } else {
            return res.toArray(new AlterCategoryVO[res.size()]);
        }
    }

}
